/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { CSSProperties, useEffect, useRef, useState } from 'react';
import { message, Space } from 'antd';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import KbPlatSetPriceModal from '../KbPlatSetPriceModal';
import { getPriceRender, getPlatformRatio, AccountType } from '../../../addLeague/_utils';
import { getDelivery } from '@/services/jdk/register';
import ProTable from '@ant-design/pro-table';
import styles from './index.less';

interface KbPlatTableProps {
  league_id?: string | any;
  user_id?: string;
  type?: AccountType; // 商家|服务商
  style?: CSSProperties;
}

const KbPlatTable: React.FC<KbPlatTableProps> = (props) => {
  const { user_id = '', league_id, style, type = 'league' } = props;
  const [record, setRecord] = useState([]);
  const actionRef = useRef<ActionType>();
  const [columns, setColumns] = useState<ProColumns<any>[]>();

  const requestTableData = async () => {
    const { code, data, msg } = await getDelivery({ user_id, league_id, type });
    if (code == 0) {
      const { head = [], list = {} } = data || {};
      const columnsList: any = [];
      const priceRenderData: any = {};
      const commissionRenderData: any = {};
      const formRenderData: any = [];
      head.unshift({
        name: '运力名称',
        code: 'desc',
      });
      priceRenderData['id'] = 1;
      priceRenderData['desc'] = '加价额度';
      commissionRenderData['id'] = 2;
      commissionRenderData['desc'] = '加价分佣';
      Object.keys(list).forEach((item) => {
        const price = getPriceRender(list[item].price) || '';
        const formRatio = list[item].ratio;
        const [ratio1, ratio] = getPlatformRatio(formRatio);
        priceRenderData[item] = price ? '¥' + price : '';
        commissionRenderData[item] = formRatio ? ratio + ':' + ratio1 : '';
        const brand_zh =
          head.find((iitem: { code: string; name: string }) => iitem.code === item).name || '';
        formRenderData.push({
          price,
          ratio,
          ratio1,
          brand_zh,
          brand: item,
        });
      });
      head.map((item: { code: string; name: string }) => {
        columnsList.push({
          width: 150,
          title: item.name,
          dataIndex: item.code,
          align: 'center',
          search: false,
          ellipsis: true,
        });
      });
      setRecord(formRenderData);
      setColumns(columnsList);
      return { data: [priceRenderData, commissionRenderData] };
    } else {
      message.error(msg);
      return {};
    }
  };

  useEffect(() => {
    if (league_id) {
      actionRef.current?.reload();
    }
  }, [league_id]);

  return (
    <ProTable
      cardProps={{
        title: <Space>平台运力加价设置</Space>,
        extra: [
          <KbPlatSetPriceModal
            key='set'
            actionRef={actionRef}
            record={record}
            user_id={user_id}
            league_id={league_id}
            formData={{}}
            type={type}
          >
            <a>设置</a>
          </KbPlatSetPriceModal>,
        ],
        style,
      }}
      rowKey='id'
      className={styles.platTable}
      actionRef={actionRef}
      pagination={false}
      search={false}
      columns={columns}
      options={false}
      request={requestTableData}
      manualRequest
      scroll={{ x: '100%' }}
    />
  );
};

export default KbPlatTable;
